package praktikumid.w10.p1;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

/**
 * Drawing panel. All the drawing takes
 * place on that panel.
 * @author Ago
 *
 */
public class DrawPanel extends JPanel {
	/**
	 * List of shapes on the panel. This
	 * list is used to draw shapes
	 * on the panel.
	 */
	List<Shape> shapes = new ArrayList<Shape>();
	
	/**
	 * An example how to keep history (for undo/redo).
	 * More about that in the next lesson.
	 */
	List<List<Shape>> history = new ArrayList<List<Shape>>();
	
	
	/**
	 * Listener for all the mouse and keyboard events.
	 * 
	 * @author Ago
	 *
	 */
	class DrawListener extends MouseAdapter implements KeyListener {
		@Override
		public void mouseDragged(MouseEvent e) {
			// if mouse is dragged (button is down and mouse moves).
			getLastShape().drag(e);
			repaint();
		}
		
		@Override
		public void mouseReleased(MouseEvent e) {	
			// when mouse button is released
			getLastShape().release(e);
			repaint();
		}
		
		@Override
		public void mouseClicked(MouseEvent e) {
			// when mouse is clicked
			// this event is called AFTER mouseReleased().
			if (e.getButton() == MouseEvent.BUTTON3) {
				Shape s = getLastShape(false);
				s.click(e);
			} else {
				getLastShape().click(e);
			}
			repaint();
		}

		@Override
		public void keyPressed(KeyEvent e) {
			// when keyboard key is pressed
			System.out.println(e.getKeyCode());
			if (e.getKeyCode() == KeyEvent.VK_CONTROL) {
				Shape s = getLastShape(false);
				if (s != null) {
					s.move(-10, 0);
					repaint();
				}
			}
			
			
		}

		@Override
		public void keyReleased(KeyEvent e) {
			// we need to implement those methods here
			// because of the KeyListener interface
			
		}

		@Override
		public void keyTyped(KeyEvent e) {

			
		}
	}
	
	public DrawPanel() {
		setBackground(Color.WHITE);
		
		shapes.add(new Circle());
		
		// we use the same listener
		// for all the cases
		DrawListener l = new DrawListener();
		addMouseListener(l);
		addMouseMotionListener(l);
		addKeyListener(l);
		
		// By default, JPanel does is not focused.
		// In order to listen key events (key pressed etc)
		// we need to get focus. Therefore, we first
		// need to make panel focusable.
		setFocusable(true);
		// after that, we need to request focus for
		// the panel.
		requestFocus();
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		System.out.println("shapes size:" + shapes.size());
		for (Shape s : shapes) {
			// when drawing, we just call
			// paintComponent method for every shape.
			s.paintComponent(g);
		}
	}
	
	/**
	 * Returns the last shape.
	 * Check p2 and p5 packages for 
	 * different examples. 
	 * @param createNew Whether new shape should be created
	 * @return The last shape
	 */
	public Shape getLastShape(boolean createNew) {
		if (shapes.size() == 0) return null;
		Shape s = null;
		if (!createNew) {
			s = shapes.get(shapes.size() - 2);
		} else {
			s = shapes.get(shapes.size() - 1);
			if (s.isFinished()) {
				// if last one is finished, create a new one
				s = new Circle();
				s = new ClosedShape();
				shapes.add(s);
			}
		}
		return s;
	}
	
	/**
	 * Overloading. If we want to say
	 * that default call for the getLastShape()
	 * without arguments is actually call to
	 * the getLastShape(true).
	 * In java there are no default values for
	 * function/method arguments, therefore overloading
	 * is used.
	 * @return
	 */
	public Shape getLastShape() {
		return getLastShape(true);
	}
}
